#include "srsupervisorwidget.h"
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QRegExp>
#include <QRegExpValidator>

#include <QKeyEvent>
#include <QDebug>

#include <OEMessageBox>
SRSupervisorWidget::SRSupervisorWidget(QWidget *parent) : QWidget(parent)
{
    // 初始化
    QLabel* lab_remark = new QLabel("提示：请务必抄写或牢记主管密码，并秘密的保管！");
    labSupervisor_ = new QLabel("主管密码");
    editSupervisor_ = new QLineEdit();
    btnNext_ = new QPushButton(tr("下一步"));
    QVBoxLayout* layout = new QVBoxLayout();
    QHBoxLayout* layout_supervisor = new QHBoxLayout();

    // 输入限制规则
    QRegExp regQuote("[0-9a-zA-Z]{1,12}");
    QRegExpValidator* quote_validator = new QRegExpValidator(regQuote, this);


    QWidget* wdgt_logo = new QWidget();
    QLabel* lab_logo = new QLabel();
    lab_logo->setAlignment(Qt::AlignCenter);
    lab_logo->setPixmap(QPixmap(":/lab/logo"));
    QLabel* lab_logo_title = new QLabel("主管确认创建企业账户");
    lab_logo_title->setAlignment(Qt::AlignCenter);
    lab_logo_title->setObjectName(QString("LogoTitle"));

    // 属性设置
    editSupervisor_->setPlaceholderText(tr("请输入6 - 12位密码"));
    editSupervisor_->setEchoMode(QLineEdit::Password);
    editSupervisor_->setValidator(quote_validator);


    // 大小
    wdgt_logo->setFixedSize(300,140);
    editSupervisor_->setFixedSize(140,30);
    btnNext_->setFixedSize(100,40);
    setFixedSize(320,300);

    QGridLayout* layout_logo = new QGridLayout();


    // 布局
    layout_logo->addWidget(lab_logo,0,1,1,1);
    layout_logo->addWidget(lab_logo_title,1,1,1,1);
    QWidget* wdgt_supervisor = new QWidget();
    layout_supervisor->addWidget(labSupervisor_);
    layout_supervisor->addWidget(editSupervisor_,1,Qt::AlignLeft);
    wdgt_supervisor->setLayout(layout_supervisor);

    wdgt_logo->setLayout(layout_logo);
    layout->addWidget(wdgt_logo,0,Qt::AlignHCenter);
    layout->addWidget(wdgt_supervisor,0,Qt::AlignHCenter);
    layout->addWidget(btnNext_,0,Qt::AlignHCenter);
    layout->addWidget(lab_remark,0,Qt::AlignHCenter);


    setLayout(layout);


    // 信号链接
    connect(btnNext_,SIGNAL(clicked()),
            this,SLOT(onNext()));
}

SRSupervisorWidget::~SRSupervisorWidget()
{
#ifdef QT_DEBUG
    qDebug() << "~主管密码注册窗口被析构";
#endif
}

void SRSupervisorWidget::keyPressEvent(QKeyEvent *event) {
    if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) {
        onNext();
        event->accept();
    }
    else if (event->key() == Qt::Key_Escape) {
        if (editSupervisor_->text().isEmpty()) {
            strPswd_.clear();
            btnNext_->setText(tr("下一步"));

        }
        else
            editSupervisor_->clear();

        event->accept();
    }
}
void SRSupervisorWidget::onNext(void) {
    if (strPswd_.isEmpty()) {
        if (editSupervisor_->text().size() < 6) {
            // @remark : 需要一个提示框
            OEMessageBox::BriefMessage(this, "密码不能小于6位");
            return;
        }

        strPswd_ = editSupervisor_->text();
        editSupervisor_->clear();
        editSupervisor_->setFocus();
        labSupervisor_->setText(tr("确认密码"));
        btnNext_->setText(tr("开始创建"));
#ifdef QT_DEBUG
        qDebug() << "设置密码:" << strPswd_;
#endif
    }
    else {
        if (strPswd_ != editSupervisor_->text()) {
            // @remark : 需要一个提示框
            OEMessageBox::BriefMessage(this, "设置密码与确认密码不一致");
            return;
        }
        emit sigPswdDone(strPswd_);
#ifdef QT_DEBUG
        qDebug() << "确认密码成功:" << strPswd_;
#endif

    }
}

